package cn.com.yusys.yusp.mobile.web.rest;


import com.sunyard.client.SunEcmClientApi;
import com.sunyard.client.bean.ClientBatchBean;
import com.sunyard.client.bean.ClientBatchFileBean;
import com.sunyard.client.bean.ClientHeightQuery;
import com.sunyard.client.impl.SunEcmClientSocketApiImpl;
import com.sunyard.ecm.server.bean.*;
import com.sunyard.ws.utils.XMLUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SunEcmFileInfoQryService {

    private String ip;
    private String groupname;
    private int socketport;
    private static Logger logger = Logger.getLogger(SunEcmClientSocketApiImpl.class);


    public String getIp() {
        return ip;
    }


    public void setIp(String ip) {
        this.ip = ip;
    }


    public String getGroupname() {
        return groupname;
    }


    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }


    public int getSocketport() {
        return socketport;
    }


    public void setSocketport(int socketport) {
        this.socketport = socketport;
    }


    /**
     * @param map DocId,ModelCode,FilePartName,FileType,SunEcmUserName,SunEcmPassword
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public Map SunEcmFileInfoQry(Map map) {
        String docId = (String) map.get("DocId");
        String modelCode = (String) map.get("ModelCode");
        String filePartName = (String) map.get("FilePartName");
        String fileType = (String) map.get("FileType");
        String username = (String) map.get("SunEcmUserName");
        String password = (String) map.get("SunEcmPassword");
        logger.info("========查询文件请求数据：========" + modelCode + " " + filePartName + " " + fileType + " " + docId + " " + username + " " + password);
        List<Map> fileInfoList = qryFile(map);

        //如果filetype不为空则筛选查询结果
        if (fileType != null && !"".equals(fileType.trim())) {
            List<Map> temp = fileInfoList;
            fileInfoList = new ArrayList<Map>();
            for (Map fileinfo : temp) {
                if (fileType.equals(fileinfo.get("FileType"))) {
                    fileInfoList.add(fileinfo);
                }
            }
        }
        Map returnMap = new HashMap<>();
        returnMap.put("List", fileInfoList);
        return returnMap;
    }


    private List<Map> qryFile(Map map1) {
        List<Map> fileInfoList = new ArrayList<Map>();
        String docid, modelCode, userName, password;
        SunEcmClientApi clientApi = new SunEcmClientSocketApiImpl(ip, socketport);
        HeightQuery hq = heightQuery(
                docid = (String) map1.get("DocId"),
                modelCode = (String) map1.get("ModelCode"),
                userName = (String) map1.get("SunEcmUserName"),
                password = (String) map1.get("SunEcmPassword"),
                groupname,
                clientApi).get(0);

        if (hq.getCount() == 0) {
            logger.info("影像处理平台|查询不到该批次影像信息！");
        } else {
            BatchIndexBean indexbean = hq.getIndexBeans().get(0);

            ClientBatchBean clientBatchBean = new ClientBatchBean();
            logger.info(indexbean.getContentID());
            clientBatchBean.setModelCode(modelCode);
            clientBatchBean.setUser(userName);
            clientBatchBean.setPassWord(password);
            clientBatchBean.setBreakPoint(false);
            clientBatchBean.setOwnMD5(false);
            Map map = indexbean.getCustomMap();
            logger.info("+++++++" + indexbean.getContentID() + "+++++++++++++" + map.get("BUSI_START_DATE"));
            clientBatchBean.getIndex_Object().setContentID(indexbean.getContentID());
            // 设置文件版本，0为最新版本
            clientBatchBean.getIndex_Object().setVersion("0");
            // 设置文件创建日期
            clientBatchBean.getIndex_Object().addCustomMap("BUSI_START_DATE", (String) map.get("BUSI_START_DATE"));
            ClientBatchFileBean clientBatchFileBean = new ClientBatchFileBean();
            clientBatchFileBean.setFilePartName((String) map1.get("FilePartName"));

            clientBatchBean.addDocument_Object(clientBatchFileBean);

            try {

                String resultMsg = clientApi.queryBatch(clientBatchBean, groupname);

                logger.info("#######查询批次返回的信息[" + resultMsg + "]#######");

                List<BatchBean> batchList = XMLUtil.xml2list(XMLUtil.removeHeadRoot(resultMsg), BatchBean.class);

                logger.info("影像报文" + resultMsg);
                for (int i = 0; i < batchList.size(); i++) {
                    BatchBean batchBean = batchList.get(i);
                    List<BatchFileBean> batchFileBeans = batchBean
                            .getDocument_Objects();
                    for (int j = 0; j < batchFileBeans.size(); j++) {
                        BatchFileBean batchFileBean = batchFileBeans.get(j);
                        List<FileBean> filebeans = batchFileBean.getFiles();
                        // 循环文件数量
                        for (int k = 0; k < filebeans.size(); k++) {
                            FileBean fileBean = filebeans.get(k);
                            // 获取影像后缀名
                            String ftype = fileBean.getOtherAtt().get("BUSI_FILE_TYPE");

                            String fpageNum = fileBean.getOtherAtt().get("BUSI_FILE_PAGENUM");
                            // 获取影像大小
                            String fsize = fileBean.getFilesize();
                            // 获取影像URL
                            String furl = fileBean.getUrl();

                            String fcnName = fileBean.getOtherAtt().get("FILE_CN_NAME");
                            // 文件类型
                            String fformat = fileBean.getFileFormat();
                            // 文件名
                            String fNo = fileBean.getFileNO();


                            logger.info("--文件业务类型--" + ftype + "--文件类型--" + fformat + "--文件名--" + fcnName + "--文件大小--" + fsize + "上传时间" + map.get("BUSI_START_DATE") + "--URL--" + furl);
                            Map<String, String> fileInfo = new HashMap<String, String>();
                            fileInfo.put("FileUrl", furl);
                            fileInfo.put("FileNo", fNo);
                            fileInfo.put("Fformat", fformat);
                            fileInfo.put("FileType", ftype);
                            fileInfo.put("FileCNName", fcnName);
                            fileInfo.put("FilePageNum", fpageNum);

                            fileInfoList.add(fileInfo);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return fileInfoList;

    }


    public List<HeightQuery> heightQuery(String docId, String modelCode, String userName, String password, String groupName, SunEcmClientApi clientApi) {
        ClientHeightQuery heightQuery = new ClientHeightQuery();
        List<HeightQuery> hqList = null;
        heightQuery.setModelCode(modelCode);
        heightQuery.setUserName(userName);
        heightQuery.setPassWord(password);
        // 设置每页数量
        heightQuery.setLimit(5);
        // 设置显示第几页，本项目只有一条信息展示第一页就可以
        heightQuery.setPage(1);
        heightQuery.addCustomAtt("BUSI_SERIAL_NO", docId);

        try {

            String resultMsg = clientApi.heightQuery(heightQuery, groupName);
            logger.info("heightQuery##|" + resultMsg);
            hqList = XMLUtil.xml2list(XMLUtil.removeHeadRoot(resultMsg), HeightQuery.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return hqList;
    }

}
